package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.dao.RoleResourceMapper;
import com.woniuxy.entity.Account;
import com.woniuxy.entity.Resource;
import com.woniuxy.dao.ResourceMapper;
import com.woniuxy.entity.RoleResource;
import com.woniuxy.exception.account.AccountException;
import com.woniuxy.exception.account.AccountExceptionCode;
import com.woniuxy.service.AccountService;
import com.woniuxy.service.ResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.RoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.channels.AcceptPendingException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2023年06月21日
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {

    @Autowired
    private AccountService accountService;

    @Autowired
    private RoleResourceService roleResourceService;

    @Override
    public List<Resource> getMenusById(Integer accountId) {
        if (accountId==null){
            throw new AccountException(AccountExceptionCode.ACCOUNT_NOT_LOGIN);
        }
        Account account = accountService.getById(accountId.longValue());
        if (account==null){
            throw new AccountException(AccountExceptionCode.ACCOUNT_NOT_LOGIN);
        }
        String role = account.getRole();
        QueryWrapper<RoleResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("resource_id");
        queryWrapper.eq("role",role);
        List<RoleResource> list = roleResourceService.list(queryWrapper);
        List<Long> resourcesId = new ArrayList<>();
        for (RoleResource roleResource : list) {
            resourcesId.add(roleResource.getResourceId());
        }
        List<Resource> resources = listByIds(resourcesId);


        return resources;
    }
}
